package com.msb.servicemap.remote;

import com.msb.internalcommon.constant.AmapConfigConstants;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.reponese.ServiceResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: jiudianliu
 * @Date: 2023/7/7 - 07 - 07 - 18:54
 * @Description: com.msb.servicemap.remote
 * @version: 1.0
 */
@Service
public class ServiceClient {

    //导入yml文件中的key
    @Value("${map.key}")
    private String amapKey ;

    @Autowired
    private RestTemplate restTemplate ;

    public ResponseResult add (String name) {
        //拼装请求的url
        //网址示例：?keywords=北京&subdistrict=2&key=<用户的key>
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.SERVICE_ADD_URL);
        url.append("?");
        url.append("key=" + amapKey);
        url.append("&");
        url.append("name=" + name);
        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null , String.class);
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        String sid = data.getString("sid");
        ServiceResponse serviceResponse = new ServiceResponse();
        serviceResponse.setSid(sid);
        return ResponseResult.success(serviceResponse) ;
    }
}
